﻿/**
 * TestNameUtility.cs
 *
 * Copyright (C) 2008,  iron9light
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

using NUnit.Core;

namespace NUnitExtension.DataDriven.AddIn
{
    public static class TestNameUtility
    {
        public static void ReplaceBase(TestName testName, string s)
        {
            if(s == null)
                return;

            testName.FullName = s + "." + testName.Name;
        }

        public static void Replace(TestName testName, string newName)
        {
            if (newName == null)
                return;

            string fullNameBase = testName.FullName.Substring(0, testName.FullName.Length - testName.Name.Length);
            testName.Name = newName;
            testName.FullName = fullNameBase + newName;
        }

        public static void Insert(TestName testName, string s)
        {
            if (s == null)
                return;

            string newName = s + testName.Name;
            Replace(testName, newName);
        }

        public static void InsertNum(TestName testName, int num, int len)
        {
            string name = num.ToString();
            if(name.Length < len)
                name = new string(' ', len - name.Length) + num;
            Insert(testName, name + ' ');
        }

        public static void Append(TestName testName, string s)
        {
            if (s == null)
                return;

            string newName = testName.Name + s;
            Replace(testName, newName);
        }

        public static void Append(TestName testName, object[] args)
        {
            string argsStr = GetArgsString(args);
            Append(testName, argsStr);
        }

        public static string GetArgsString(object[] args)
        {
            if (args == null || args.Length == 0)
                return null;

            string[] strs = new string[args.Length];
            int index = 0;
            foreach (object arg in args)
            {
                strs[index++] = arg != null ? arg.ToString() : "null";
            }
            return "(" + string.Join(",", strs) + ")";
        }
    }
}
